package cn.com.fssg.platform.base.handler.impl.cl.jiesuan;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import cn.com.fssg.platform.biz.ExtendUtil;
import cn.com.fssg.platform.modules.system.entity.SysUser;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: ClDepartCtl
 * @description: TODO 类描述
 * @author: xzm
 * @date: 2025/10/20
 **/
@Component
public class ClDepartCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();
    //@Autowired
    //private NamedParameterJdbcTemplate jdbcTemplate;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String type = jsonObject.getString("type");
        //id查询
        if(StringUtils.equals(type, "0")){
            //查询
            String id = jsonObject.getString("id");
            String querySql = "SELECT us.loginname,us.id,us.realname,us.phone,us.status,us.create_time,dep.depart_name,dep.org_code,dep.id AS dept_id " +
                    "FROM pxone.su_user us INNER JOIN pxone.su_user_depart ero ON us.id = ero.user_id INNER JOIN pxone.su_depart dep ON ero.dep_id = dep.id ";
            Map<String,Object> map = new HashMap<>();
            map.put("id",id);
            if(StringUtils.isNotBlank(id)){
                querySql += " where us.id =:id";
            }
            Map<String,Object> queryMap = jdbcTemplate.queryForMap(querySql, map);
            return queryMap;
        } else if(StringUtils.equals(type, "1")){
            //添加用户
            //判断账号是否存在
            String loginname = jsonObject.getString("loginname");
            String querySql = "select count(id) from pxone.su_user where loginname =:loginname";
            Map<String,Object> queryMap = new HashMap<>();
            queryMap.put("loginname",loginname);
            Map<String,Object> queryUserMap = jdbcTemplate.queryForMap(querySql, queryMap);
            if(StringUtils.equals(queryUserMap.get("count").toString(), "1")){
                return new Exception("添加的账号已存在");
            }
            String roleSql = "select * from pxone.su_role where role_code =:role_code";
            Map<String,Object> queryRoleMap = new HashMap<>();
            queryRoleMap.put("role_code",jsonObject.getString("role_code"));
            Map<String,Object> queryRole = jdbcTemplate.queryForMap(roleSql, queryRoleMap);
            //添加
            JSONObject json = new JSONObject();
            String id = BizUtil.cur().getSnowflakeIdStr();
            json.put("id",id);
            String password = jsonObject.getString("password");
            //String salt = jsonObject.getString("salt");
            json.put("realname", jsonObject.getString("realname"));
            //String pwd = SmUtil.sm3(loginname +":"+ password);
            json.put("loginname", loginname);
            json.put("phone", jsonObject.getString("phone"));
            json.put("password", password);
            //json.put("salt", salt);
            json.put("sex", jsonObject.getLong("sex"));
            json.put("status", jsonObject.getLong("status"));

            json.put("updateBy", BizUtil.cur().curUserId());
            json.put("createBy", BizUtil.cur().curUserId());
            json.put("updateTime", new Date());
            json.put("createTime", new Date());
            json.put("del_flag", 0);
            json.put("user_identity", 1);
            json.put("create_time", new Date());
            json.put("selectedroles",queryRole.get("id"));
            json.put("selecteddeparts",jsonObject.getString("dept_id"));
            json.put("tecjrlhh","tecjrlhh");
            return ExtendUtil.cur().addUser(json);
        }else if(StringUtils.equals(type, "2")){
            //修改用户信息(除密码外)
            //String loginname = jsonObject.getString("loginname");
            String id = jsonObject.getString("id");
            SysUser user = new SysUser();
            user.setId(id);
            user.setUpdateBy(BizUtil.cur().curUserId());
            user.setUpdateTime(new Date());
            user.setLastModifyPasswordTime(new Date());
            user.setRealname(jsonObject.getString("realname"));
            //user.setLoginname(jsonObject.getString("loginname"));
            user.setPhone(jsonObject.getString("phone"));
            user.setSex(jsonObject.getInteger("sex"));
            user.setStatus(jsonObject.getInteger("status"));
            return ExtendUtil.cur().updateUserInfo(user);
        }else if(StringUtils.equals(type, "3")){
            //删除
            String batch = jsonObject.getString("batch");
            SysUser user = new SysUser();
            //单次删除
            if(StringUtils.equals(batch, "Y")){
                String id = jsonObject.getString("id");
                user.setId(id);
                user.setDelFlag(1);
                return ExtendUtil.cur().updateUserInfo(user);
            }
            //批量删除
            if(StringUtils.equals(batch, "N")){
                jsonObject.getJSONArray("id").forEach(item->{
                    user.setId(item.toString());
                    user.setDelFlag(1);
                    ExtendUtil.cur().updateUserInfo(user);
                });
            }
            return batch;

        }
        return null;
    }
}
